<?php
$availableTable = new BSheet('availableApplyRole', new BRole());
$roleIdList = $availableTable->getValueList('roleId');
$model = new BDynamicFormModel();
$memberListField = 'array_media_memberList_admin_membership';
$customerListField = 'array_media_customerList_admin_membership';
$allRoleListField = 'array_media_allUserList_admin_membership';
$model->addField($memberListField);
$model->addField($customerListField);
$model->addField($allRoleListField);

if (isset($memberRoleData))
    $model->$memberListField = $memberRoleData;
if (isset($customerRoleData))
    $model->$customerListField = $customerRoleData;
if (isset($allRoleData))
    $model->$allRoleListField = $allRoleData;
?>
<?php
$form = $this->beginWidget('CActiveForm', array(
    "id" => "manage-myInfo-form",
    'enableAjaxValidation' => FALSE,
    'htmlOptions' => array('class' => 'form-signin', 'role' => 'form'),
        ));
?>
<div class="container">
    <BR>
    <legend>设置不同角色用户可以申请的权限</legend>
    <?php if (isset($addRoleResult)) { ?>
        <?php
        if ($addRoleResult == 'success') {
            echo '<p class="bg-success">add allow apply roles successfully!</p>';
        } else {
            echo '<p class="bg-danger">add allow apply roles fail!</p>';
        }
        ?>
    <?php } ?>

    <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
        <?php
        $i = 1;
        foreach ($roleIdList as $roleId => $count) {
            $role = new BRole($roleId);
            $roleFullName = $role->getFullName();
            if ($roleFullName != 'root' && $roleFullName != 'app_root.app_admin') {
                ?>
                <div class="panel panel-default">
                    <div class="panel-heading" role="tab" id="<?php echo 'heading' . $roleId; ?>">
                        <h4 class="panel-title">
                            <a data-toggle="collapse" data-parent="#accordion" href="#collapse<?php echo $roleId; ?>" aria-expanded="true" aria-controls="collapseOne">
                                <?php
                                if ($roleFullName == 'app_root.app_member') {
                                    echo '设置机构成员可以申请的角色';
                                }
                                if ($roleFullName == 'app_root.app_customer') {
                                    echo '设置机构客户可以申请的角色';
                                }
                                if ($roleFullName == 'all_roles') {
                                    echo '设置任何比邻用户可以申请的角色';
                                }
                                ?>
                            </a>
                        </h4>
                    </div>
                    <div id="collapse<?php echo $roleId; ?>" class="panel-collapse collapse <?php
                        if ($i == 1) {
                            echo 'in';
                            $i++;
                        }
                                ?>" role="tabpanel" aria-labelledby="heading<?php echo $roleId; ?>">
                        <div class="panel-body">
                            <?php
                            $roleTable = new BSheet('availableApplyRole', $role);
                            $roleView = $roleTable->getView();
                            foreach ($roleView as $list) {
                                $applyRoleId = $list->getItem('applyRoleId')->getValue();
                                $applyRoleFullName = $list->getItem('applyRoleFullName')->getValue();
                                $appClass = $list->getItem('appClass')->getValue();
                                $applyRoleName = $list->getItem('applyRoleName')->getValue();
                                $roleData[$applyRoleId] = $appClass.'::'.$applyRoleName;
                            }
                            if (isset($roleData) && $roleData != NULL) {
                                if ($roleFullName == 'app_root.app_member')
                                    echo CHtml::activeCheckBoxList($model, $memberListField, $roleData);
                                if ($roleFullName == 'app_root.app_customer')
                                    echo CHtml::activeCheckBoxList($model, $customerListField, $roleData);
                                if ($roleFullName == 'all_roles')
                                    echo CHtml::activeCheckBoxList($model, $allRoleListField, $roleData);
                            }
                            ?>
                        </div>
                    </div>
                </div>
                <?php
            }
        }
        ?>
    </div>
    <div class="submit"> 
        <?php
        echo $control->getAjaxLink(
                array('name' => '添加允许权限', 'id' => BUIMembership::Add_Allow_Apply_Role), array(
            'htmlOptions' => array(
                'class' => 'btn btn-primary btn-lg btn-block')));
        ?>
        <?php
        echo $control->getAjaxLink(
                array('name' => '删除允许权限', 'id' => BUIMembership::Delete_Allow_Apply_Role), array(
            'htmlOptions' => array(
                'class' => 'btn btn-danger btn-lg btn-block')));
        ?>
    </div>
</div>
<?php $this->endWidget(); ?>